package com.codi.fundData.service.impl;

import com.codi.base.dataSource.annotation.DataSource;
import com.codi.base.util.DateUtils;
import com.codi.bus.constant.GlobalConstant;
import com.codi.fundData.dao.FundDividendDao;
import com.codi.fundData.domain.FundDividend;
import com.codi.fundData.service.FundDataDividendService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * 模块名
 *
 * @author spy
 * @version 1.0 2018-01-26
 * @since 1.0
 */
@Slf4j
@Service
@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
public class FundDataDividendServiceImpl implements FundDataDividendService {

    @Autowired
    private FundDividendDao fundDividendDao;

    @Override
    @DataSource(GlobalConstant.DATA_SOURCE_FUND_DETAIL)
    public FundDividend queryRecently(Integer innerCode) {

        FundDividend record = fundDividendDao.selectNewest(innerCode);

        if (record == null) {
            return record;
        }

        Date beginDate = record.getInfoPublDate();
        // 日期比较

        Date endDate = DateUtils.addDay(record.getExecuteDate(), 1);

        Date now = new Date();

        if (now.getTime() >= beginDate.getTime() && now.getTime() <= endDate.getTime()) {
            return record;
        }

        return null;
    }

    @Override
    public FundDividend query(Integer innerCode) {
        return fundDividendDao.selectNewest(innerCode);
    }
}
